<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="生成报表年月" prop="bbny" label-width="100px">
        <el-date-picker
          :editable="false"
          clearable
          size="small"
          v-model="queryParams.bbny"
          type="month"
          placeholder="生成报表年月"
          value-format="yyyy-MM"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          v-hasPermi="['productionProcess:analysis_utilities:add']"
          @click="handleAdd"
          >生成</el-button
        >
      </el-form-item>

      <el-form-item label="报表年月" prop="bbny">
        <el-date-picker
          :editable="false"
          clearable
          size="small"
          v-model="queryParams.bbny"
          type="monthrange"
          range-separator="至"
          start-placeholder="开始月份"
          end-placeholder="结束月份"
          value-format="yyyy-MM"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="报表编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入报表编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报表名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入报表名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['productionProcess:analysis_utilities:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['productionProcess:analysis_utilities:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['productionProcess:analysis_utilities:remove']"
          >删除</el-button
        >
      </el-col>
      <!--  <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['productionProcess:analysis_utilities:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="analysis_utilitiesList"
      @selection-change="handleSelectionChange"
      max-height="500"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="报表年月" align="center" prop="bbny" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bbny, "{y}-{m}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="报表编号" align="center" prop="code" />
      <el-table-column label="报表名称" align="center" prop="name" /> -->
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="excelhandleView(scope.row)"
            >导出</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['productionProcess:analysis_utilities:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['productionProcess:analysis_utilities:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仰口达意隆电费用对话框 -->
    <el-dialog
      fullscreen
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="报表年月" prop="bbny">
              <el-date-picker
                :editable="false"
                clearable
                size="small"
                v-model="form.bbny"
                type="month"
                value-format="yyyy-MM"
                placeholder="选择报表年月"
                :disabled="openDisabled"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <!--  <el-col :span="12">
                <el-form-item label="报表编号" prop="code">
                  <el-input v-model="form.code" placeholder="请输入报表编号" :disabled="openDisabled" />
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="报表名称" prop="name">
                  <el-input v-model="form.name" placeholder="请输入报表名称" :disabled="openDisabled" />
                </el-form-item>
            </el-col>
            <el-col :span="24">
                <el-form-item label="备注" prop="remark">
                  <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="openDisabled" />
                </el-form-item>
            </el-col> -->
        </el-row>
        <!--  <el-divider content-position="center">仰口达意隆电费用子信息</el-divider> -->
        <!-- <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddRptAnalysisUtilitiesSub" :disabled="openDisabled">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteRptAnalysisUtilitiesSub" :disabled="openDisabled">删除</el-button>
          </el-col>
        </el-row> -->
        <el-table
          :data="rptAnalysisUtilitiesSubList"
          :row-class-name="rowRptAnalysisUtilitiesSubIndex"
          @selection-change="handleRptAnalysisUtilitiesSubSelectionChange"
          ref="rptAnalysisUtilitiesSub"
        >
          <!-- <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/> -->
          <el-table-column prop="month" label="日期">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.month"
                placeholder="请输入报表月份"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column label="达意隆线产量(标准箱)" prop="dylcl">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.dylcl"
                placeholder="请输入达意隆线产量(标准箱)"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column label="达意隆线耗电(千瓦时)" prop="dylhd">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.dylhd"
                placeholder="请输入达意隆线耗电(千瓦时)"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column label="达意隆线单箱耗电(千瓦时)" prop="dyldxhd">
            <template slot-scope="scope">
              <el-input
                type="number"
                v-model="scope.row.dyldxhd"
                placeholder="请输入达意隆线单箱耗电(千瓦时)"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <!-- <el-table-column label="备注" prop="remark">
            <template slot-scope="scope">
                <el-input v-model="scope.row.remark" type="textarea" placeholder="请输入内容" :disabled="openDisabled"/>
            </template>
          </el-table-column> -->
        </el-table>
      </el-form>
      <br /><br />
      <!-- 图表 -->
      <!-- 为ECharts准备一个具备大小（宽高）的Dom -->
      <el-row :gutter="5">
        <el-col :span="8">
          <div ref="main1" style="width: 550px; height: 300px"></div>
        </el-col>
        <el-col :span="8">
          <div ref="main2" style="width: 550px; height: 300px"></div>
        </el-col>
        <el-col :span="8">
          <div ref="main3" style="width: 550px; height: 300px"></div>
        </el-col>
      </el-row>

      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAnalysis_utilities,
  getAnalysis_utilities,
  delAnalysis_utilities,
  addAnalysis_utilities,
  updateAnalysis_utilities,
} from "@/api/report/analysis/analysis_utilities";

export default {
  name: "Analysis_utilities",
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedRptAnalysisUtilitiesSub: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仰口达意隆电费用表格数据
      analysis_utilitiesList: [],
      // 仰口达意隆电费用子表格数据
      rptAnalysisUtilitiesSubList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        bbny: null,
        bbny1: null,
        code: null,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /* 初始化echarts实例  */
    initChart(res) {
      const arr = res.data.rptAnalysisUtilitiesSubList;
      // console.log(arr);
      this.$nextTick(() => {
        //基于准备好的dom，初始化echarts实例
        let myChart1 = this.$echarts.init(this.$refs.main1);
        let myChart2 = this.$echarts.init(this.$refs.main2);
        let myChart3 = this.$echarts.init(this.$refs.main3);

        //  中间对option数据进行处理
        let option1 = {
          backgroundColor: "#407EB4",
          title: {
            text: "达意隆线产量(标准箱)",
          },
          xAxis: {
            type: "category",
            data: [arr[0].month, arr[3].month],
          },
          yAxis: {
            type: "value",
          },
          grid: {
            left: "5%",
            right: "5%",
            bottom: "5%",

            containLabel: true,
          },
          series: [
            {
              data: [
                {
                  value: arr[0].dylcl,
                  itemStyle: {
                    color: "#08D66F",
                  },
                },
                arr[3].dylcl,
              ],
              barWidth: 50,
              itemStyle: {
                //上方显示数值
                normal: {
                  label: {
                    show: true, //开启显示
                    position: "top", //在上方显示
                    textStyle: {
                      //数值样式
                      // color: "white",
                      fontSize: 16,
                    },
                  },
                },
              },
              type: "bar",
            },
          ],
        };
        // 使用刚指定的配置项和数据显示图表。
        myChart1.setOption(option1);

        //  中间对option数据进行处理
        let option2 = {
          backgroundColor: "#407EB4",
          title: {
            text: "达意隆线耗电(千瓦时)",
          },
          xAxis: {
            type: "category",
            data: [arr[0].month, arr[3].month],
          },
          yAxis: {
            type: "value",
          },
          grid: {
            left: "5%",
            right: "5%",
            bottom: "5%",

            containLabel: true,
          },
          series: [
            {
              data: [
                {
                  value: arr[0].dylhd,
                  itemStyle: {
                    color: "#08D66F",
                  },
                },

                arr[3].dylhd,
              ],
              barWidth: 50,
              itemStyle: {
                //上方显示数值
                normal: {
                  label: {
                    show: true, //开启显示
                    position: "top", //在上方显示
                    textStyle: {
                      //数值样式
                      // color: "white",
                      fontSize: 16,
                    },
                  },
                },
              },
              type: "bar",
            },
          ],
        };
        // 使用刚指定的配置项和数据显示图表。
        myChart2.setOption(option2);

        //  中间对option数据进行处理
        let option3 = {
          backgroundColor: "#407EB4",
          title: {
            text: "达意隆线单箱耗电(千瓦时)",
          },
          xAxis: {
            type: "category",
            data: [arr[0].month, arr[3].month],
          },
          grid: {
            left: "5%",
            right: "5%",
            bottom: "5%",

            containLabel: true,
          },
          yAxis: {
            type: "value",
          },
          series: [
            {
              data: [
                {
                  value: arr[0].dyldxhd,
                  itemStyle: {
                    color: "#08D66F",
                  },
                },
                arr[3].dyldxhd,
              ],
              barWidth: 50,
              itemStyle: {
                //上方显示数值
                normal: {
                  label: {
                    show: true, //开启显示
                    position: "top", //在上方显示
                    textStyle: {
                      //数值样式
                      // color: "white",
                      fontSize: 16,
                    },
                  },
                },
              },
              type: "bar",
            },
          ],
        };
        // 使用刚指定的配置项和数据显示图表。
        myChart3.setOption(option3);
      });
    },
    /** 查询仰口达意隆电费用列表 */
    getList() {
      this.loading = true;
      listAnalysis_utilities(this.queryParams).then((response) => {
        this.analysis_utilitiesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bbny: null,
        code: null,
        name: null,
        delFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.rptAnalysisUtilitiesSubList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.bbny != null) {
        let arr = this.queryParams.bbny;
        this.queryParams.bbny = arr[0];
        this.queryParams.bbny1 = arr[1];
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // this.openDisabled = false;
      // this.open = true;
      // this.title = "仰口达意隆电";

      if (this.queryParams.bbny == null || this.queryParams.bbny == "") {
        this.msgError("请输入生产报表年月");
      } else if (Array.isArray(this.queryParams.bbny)) {
        this.msgError("请重置筛选日期");
      } else {
        addAnalysis_utilities(this.queryParams).then((response) => {
          this.msgSuccess("新增成功");
          // this.open = false;
          // rLoading.close();
          this.resetQuery();
          this.getList();
        });
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getAnalysis_utilities(id).then((response) => {
        this.form = response.data;
        this.rptAnalysisUtilitiesSubList =
          response.data.rptAnalysisUtilitiesSubList;
        this.initChart(response);
        this.open = true;
        this.title = "修改仰口达意隆电";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;

      const id = row.id || this.ids;
      getAnalysis_utilities(id).then((response) => {
        this.form = response.data;
        this.rptAnalysisUtilitiesSubList =
          response.data.rptAnalysisUtilitiesSubList;
        this.initChart(response);
        this.open = true;
        this.title = "查看仰口达意隆电";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.rptAnalysisUtilitiesSubList =
            this.rptAnalysisUtilitiesSubList;
          if (this.form.id != null) {
            updateAnalysis_utilities(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const rLoading = this.openLoading();
            addAnalysis_utilities(this.form)
              .then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                rLoading.close();
                this.getList();
              })
              .catch((error) => {
                rLoading.close();
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除仰口达意隆电编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delAnalysis_utilities(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 仰口达意隆电费用子序号 */
    rowRptAnalysisUtilitiesSubIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 仰口达意隆电费用子添加按钮操作 */
    handleAddRptAnalysisUtilitiesSub() {
      let obj = {};
      obj.month = "";
      obj.dylcl = "";
      obj.dylhd = "";
      obj.dyldxhd = "";
      obj.remark = "";
      this.rptAnalysisUtilitiesSubList.push(obj);
    },
    /** 仰口达意隆电费用子删除按钮操作 */
    handleDeleteRptAnalysisUtilitiesSub() {
      if (this.checkedRptAnalysisUtilitiesSub.length == 0) {
        this.$alert("请先选择要删除的仰口达意隆电子数据", "提示", {
          confirmButtonText: "确定",
        });
      } else {
        this.rptAnalysisUtilitiesSubList.splice(
          this.checkedRptAnalysisUtilitiesSub[0].index - 1,
          1
        );
      }
    },
    /** 单选框选中数据 */
    handleRptAnalysisUtilitiesSubSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.rptAnalysisUtilitiesSub.clearSelection();
        this.$refs.rptAnalysisUtilitiesSub.toggleRowSelection(selection.pop());
      } else {
        this.checkedRptAnalysisUtilitiesSub = selection;
      }
    },
    excelhandleView(row) {
      this.download(
        "productionProcess/analysis_utilities/exportexcel?id=" + row.id,
        {
          ...this.queryParams,
        },
        "仰口达意隆电" + this.parseTime(new Date()) + ".xlsx"
      );
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "productionProcess/analysis_utilities/export",
        {
          ...this.queryParams,
        },
        "仰口达意隆电" + this.parseTime(new Date()) + ".xlsx"
      );
    },
  },
};
</script>
